package com.ykht.proj.activity.mq.init;

import com.ykht.proj.activity.mq.MqConfig;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

@Data
@Slf4j
@Component
public class Producer {
	private  DefaultMQProducer defProducer;

	@Resource
	private MqConfig mqConfig;

	@PostConstruct
	public void init(){
		defProducer = new DefaultMQProducer(mqConfig.getProducerGroup());
		try {
			// 设置NameServer的地址
			defProducer.setNamesrvAddr(mqConfig.getNamasrv());
//			producer.setVipChannelEnabled(false);
			// 启动Producer实例
			defProducer.start();
			log.info("启动生产者->生产者组:{}",defProducer.getProducerGroup());
		} catch (Exception e) {
			e.printStackTrace();
			// 如果不再发送消息，关闭Producer实例。
			defProducer.shutdown();
		}
	}
}
